Socket
Socket
Sign inDemoInstall

pretty-format

Package Overview
Dependencies
1
Maintainers
3
Versions
236
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    pretty-format

Stringify any JavaScript value.


Version published
Weekly downloads
83M
increased by5.03%
Maintainers
3
Install size
286 kB
Created
Weekly downloads
 

Package description

What is pretty-format?

The pretty-format npm package is a JavaScript library that allows you to serialize any JavaScript value into a string with a human-readable format. It is particularly useful for snapshot testing, where you want to compare the expected and actual output of your test cases.

What are pretty-format's main functionalities?

Pretty-printing of basic JavaScript types

This feature allows you to convert basic JavaScript types like objects, arrays, strings, numbers, etc., into a nicely formatted string.

const prettyFormat = require('pretty-format');
const value = { foo: 'bar', baz: 42 };
console.log(prettyFormat(value));

Customizing output with plugins

pretty-format supports plugins that can be used to customize the output for specific types of values, such as React elements.

const prettyFormat = require('pretty-format');
const ReactElementPlugin = require('pretty-format/plugins/ReactElement');
const reactElement = <div>Hello World</div>;
console.log(prettyFormat(reactElement, { plugins: [ReactElementPlugin] }));

Minimizing diff output

By using pretty-format in combination with a diffing library like jest-diff, you can minimize the output of diffs to make them easier to read and understand.

const prettyFormat = require('pretty-format');
const diff = require('jest-diff');
const oldValue = { a: 'old', b: 'values' };
const newValue = { a: 'new', b: 'values' };
const difference = diff(prettyFormat(oldValue), prettyFormat(newValue));
console.log(difference);

Other packages similar to pretty-format

Changelog

Source

jest 18.1.0

  • Fixed console.log and fake timer behavior in node 7.3.
  • Updated istanbul-api.
  • Updated jest-diff equality error message.
  • Disabled arrow keys when entering a pattern in watch mode to prevent broken behavior. Will be improved in a future release.
  • Moved asymmetric matchers and equality functionality from Jasmine into jest-matchers.
  • Removed jasmine and jest-snapshot dependency from jest-matchers.
  • Removed unused global context variable.
  • Show a better error message if the config is invalid JSON.
  • Highlight trailing whitespace in assertion diffs and snapshots.
  • Jest now uses micromatch instead of minimatch.
  • Added -h as alias for --help.

Readme

Source

pretty-format

Stringify any JavaScript value.

  • Supports all built-in JavaScript types
  • Blazingly fast (similar performance to v8's JSON.stringify and significantly faster than Node's util.format)
  • Plugin system for extending with custom types (i.e. ReactTestComponent)

Installation

$ yarn add pretty-format

Usage

const prettyFormat = require('pretty-format');

var obj = { property: {} };
obj.circularReference = obj;
obj[Symbol('foo')] = 'foo';
obj.map = new Map();
obj.map.set('prop', 'value');
obj.array = [1, NaN, Infinity];

console.log(prettyFormat(obj));

Result:

Object {
  "property": Object {},
  "circularReference": [Circular],
  "map": Map {
    "prop" => "value"
  },
  "array": Array [
    1,
    NaN,
    Infinity
  ],
  Symbol(foo): "foo"
}
Type Support

Object, Array, ArrayBuffer, DataView, Float32Array, Float64Array, Int8Array, Int16Array, Int32Array, Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array, arguments, Boolean, Date, Error, Function, Infinity, Map, NaN, null, Number, RegExp, Set, String, Symbol, undefined, WeakMap, WeakSet

API

console.log(prettyFormat(object));
console.log(prettyFormat(object, options));

Options:

  • callToJSON
    Type: boolean, default: true
    Call toJSON() on passed object.
  • indent
    Type: number, default: 2
    Number of spaces for indentation.
  • maxDepth
    Type: number, default: Infinity
    Print only this number of levels.
  • min
    Type: boolean, default: false
    Print without whitespace.
  • plugins
    Type: array, default: []
    Plugins (see the next section).
  • printFunctionName
    Type: boolean, default: true
    Print function names or just [Function].
  • escapeRegex
    Type: boolean, default: false
    Escape special characters in regular expressions.
  • highlight
    Type: boolean, default: false
    Highlight syntax for terminal (works only with ReactTestComponent and ReactElement plugins.
  • theme
    Type: object, default: {tag: 'cyan', content: 'reset'...}
    Syntax highlight theme.
    Uses ansi-styles colors + reset for no color.
    Available types: tag, content, prop and value.

Plugins

Pretty format also supports adding plugins:

const fooPlugin = {
  test(val) {
    return val && val.hasOwnProperty('foo');
  },
  print(val, print, indent) {
    return 'Foo: ' + print(val.foo);
  }
};

const obj = {foo: {bar: {}}};

prettyFormat(obj, {
  plugins: [fooPlugin]
});
// Foo: Object {
//   "bar": Object {}
// }
ReactTestComponent and ReactElement plugins
const prettyFormat = require('pretty-format');
const reactTestPlugin = require('pretty-format/build/plugins/ReactTestComponent');
const reactElementPlugin = require('pretty-format/build/plugins/ReactElement');

const React = require('react');
const renderer = require('react-test-renderer');

const element = React.createElement('h1', null, 'Hello World');

prettyFormat(renderer.create(element).toJSON(), {
  plugins: [reactTestPlugin, reactElementPlugin]
});
// <h1>
//   Hello World
// </h1>

FAQs

Last updated on 29 Dec 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc